package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.TbPreflightBo;
import com.ruoyi.tb.lmt.domain.vo.TbPreflightVo;
import com.ruoyi.tb.lmt.domain.TbPreflight;
import com.ruoyi.tb.lmt.mapper.TbPreflightMapper;
import com.ruoyi.tb.lmt.service.ITbPreflightService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 预检主表Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-03
 */
@RequiredArgsConstructor
@Service
public class TbPreflightServiceImpl implements ITbPreflightService {

    private final TbPreflightMapper baseMapper;

    /**
     * 查询预检主表
     */
    @Override
    public TbPreflightVo queryById(Long ID){
        return baseMapper.selectVoById(ID);
    }

    /**
     * 查询预检主表列表
     */
    @Override
    public TableDataInfo<TbPreflightVo> queryPageList(TbPreflightBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbPreflight> lqw = buildQueryWrapper(bo);
        Page<TbPreflightVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询预检主表列表
     */
    @Override
    public List<TbPreflightVo> queryList(TbPreflightBo bo) {
        LambdaQueryWrapper<TbPreflight> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TbPreflight> buildQueryWrapper(TbPreflightBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbPreflight> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getLmtType()), TbPreflight::getLmtType, bo.getLmtType());
        lqw.eq(StringUtils.isNotBlank(bo.getLmtNumber()), TbPreflight::getLmtNumber, bo.getLmtNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkerteam()), TbPreflight::getWorkerteam, bo.getWorkerteam());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkerteamCharge()), TbPreflight::getWorkerteamCharge, bo.getWorkerteamCharge());
        lqw.eq(StringUtils.isNotBlank(bo.getTelphone()), TbPreflight::getTelphone, bo.getTelphone());
        lqw.eq(bo.getMachineGroupPepoles() != null, TbPreflight::getMachineGroupPepoles, bo.getMachineGroupPepoles());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckMain()), TbPreflight::getCheckMain, bo.getCheckMain());
        lqw.eq(bo.getRequireStartTime() != null, TbPreflight::getRequireStartTime, bo.getRequireStartTime());
        lqw.eq(bo.getRequireEndTime() != null, TbPreflight::getRequireEndTime, bo.getRequireEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getPlanPerson()), TbPreflight::getPlanPerson, bo.getPlanPerson());
        lqw.eq(StringUtils.isNotBlank(bo.getApprovalUser()), TbPreflight::getApprovalUser, bo.getApprovalUser());
        lqw.eq(bo.getSendTime() != null, TbPreflight::getSendTime, bo.getSendTime());
        lqw.eq(StringUtils.isNotBlank(bo.getSendUser()), TbPreflight::getSendUser, bo.getSendUser());
        lqw.eq(StringUtils.isNotBlank(bo.getCodeNumber()), TbPreflight::getCodeNumber, bo.getCodeNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TbPreflight::getStatus, bo.getStatus());
        lqw.eq(bo.getLastCheckTime() != null, TbPreflight::getLastCheckTime, bo.getLastCheckTime());
        lqw.eq(StringUtils.isNotBlank(bo.getLastCheckUser()), TbPreflight::getLastCheckUser, bo.getLastCheckUser());
        lqw.eq(StringUtils.isNotBlank(bo.getRepairUnit()), TbPreflight::getRepairUnit, bo.getRepairUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getPlaceLine()), TbPreflight::getPlaceLine, bo.getPlaceLine());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkgroupTag()), TbPreflight::getWorkgroupTag, bo.getWorkgroupTag());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkType()), TbPreflight::getWorkType, bo.getWorkType());
        return lqw;
    }

    /**
     * 新增预检主表
     */
    @Override
    public Boolean insertByBo(TbPreflightBo bo) {
        TbPreflight add = BeanUtil.toBean(bo, TbPreflight.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setID(add.getID());
        }
        return flag;
    }

    /**
     * 修改预检主表
     */
    @Override
    public Boolean updateByBo(TbPreflightBo bo) {
        TbPreflight update = BeanUtil.toBean(bo, TbPreflight.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbPreflight entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除预检主表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
